package com.easysoft.framework.action;

import com.easysoft.commons.utils.FileUtils;
import com.easysoft.commons.utils.StringUtils;
import com.easysoft.commons.utils.WebConstants;
import com.easysoft.framework.annotation.logs.SystemControllerLog;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.enterprise.model.SysEnterprise;
import com.easysoft.framework.enterprise.model.SysEnterpriseQuery;
import com.easysoft.framework.enterprise.model.SysEnterpriseView;
import com.easysoft.framework.enterprise.service.SysEnterpriseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping(value = SystemEnterpriseAction.BASE_VIEW)
public class SystemEnterpriseAction {
    public static final String BASE_VIEW = "/enterprise";
    private static final Logger logger = LoggerFactory.getLogger(SystemEnterpriseAction.class);

    @Autowired
    @Qualifier("service.SysEnterpriseService")
    private SysEnterpriseService sysEnterpriseService;


    @RequestMapping(value = "/listEnterprise", method = RequestMethod.GET)
    public ModelAndView listEnterprise(Page page, SysEnterpriseQuery query, HttpSession session) {
        logger.info("Call SystemEnterpriseAction.listManager({},{})", page.toString(), query.toString());
        List<SysEnterpriseView> listEnterprise = sysEnterpriseService.selectSysProductByPage(page, query);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/listEnterprise");
        mv.addObject("page", page);
        mv.addObject("query", query);
        mv.addObject("listEnterprise", listEnterprise);
        return mv;
    }

    @RequestMapping(value = "/editEnterprise")
    public ModelAndView editEnterprise(String action, @RequestParam(required = false) String id) {
        logger.info("Call SystemEnterpriseAction.editEnterprise({},{})", action, id);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/editEnterprise");
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(id)) {
                return null;
            }
            return mv;
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(id)) {
                return null;
            }
            SysEnterprise entity = sysEnterpriseService.selectSysEnterpriseById(id);
            if (entity == null) {
                return null;
            }
            mv.addObject("entity", entity);
            return mv;
        }
        return null;
    }

    @RequestMapping(value = "/saveEnterprise", method = RequestMethod.POST)
    public int saveEnterprise(String action, SysEnterprise enterprise, @RequestParam(value = "logo", required = false) MultipartFile logo, @RequestParam(value = "micromsg", required = false) MultipartFile micromsg, HttpServletRequest request, HttpSession session) {
        logger.info("Call SystemManagerAction.saveManager({},{})", action, enterprise.toString());
        if (logo != null && !("").equals(logo)) {
            enterprise.setLogo(FileUtils.savePicture(logo, "enterprise", request));
        }
        if (micromsg != null && !("").equals(micromsg)) {
            enterprise.setMicromsg(FileUtils.savePicture(micromsg, "enterprise", request));
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(enterprise.getId())) {
                return 0;
            }
            return sysEnterpriseService.addSysEnterprise(enterprise);
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(enterprise.getId())) {
                return 0;
            }
            return sysEnterpriseService.updateSysEnterprise(enterprise);
        }
        return 0;
    }

    @SystemControllerLog(description = "新增企业信息")
    public int addSysEnterprise(SysEnterprise enterprise, HttpSession session) {
        logger.info("Call SystemManagerAction.addSysEnterprise({})", enterprise.toString());
        enterprise.setOperator((String) session.getAttribute(WebConstants.SESSION_USER_ID));
        return sysEnterpriseService.addSysEnterprise(enterprise);
    }
    @SystemControllerLog(description = "修改企业信息")
    @RequestMapping(value = "/updateSysEnterprise", method = RequestMethod.POST)
    public int updateSysEnterprise(SysEnterprise enterprise, HttpSession session) {
        logger.info("Call SystemManagerAction.updateSysEnterprise({})", enterprise.toString());
        enterprise.setOperator((String) session.getAttribute(WebConstants.SESSION_USER_ID));
        return sysEnterpriseService.updateSysEnterprise(enterprise);
    }

    @RequestMapping(value = "/deleteEnterprise", method = RequestMethod.POST)
    @SystemControllerLog(description = "删除企业信息")
    public int deleteSysEnterprise(String id) {
        logger.info("Call SystemManagerAction.deleteSysEnterprise({})", id);
        return sysEnterpriseService.deleteSysEnterprise(id);
    }
}
